<template>
	<view>
		<view class="goods-list">
			<!-- block不会被渲染成其他组件，只起包裹作用 -->
			<view v-for="(goods,i) in goodList" :key="i" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 商品列表数据
				goodList : [],
				// 总页数
				total : 0,
				// 节流阀
				isloading : false,

				// 请求参数对象
				queryObj : {
					// 关键词
					query : '',
					//商品id
					cid : '',
					// 页码数
					pagenum : 1,
					//每页显示的数量
					pagesize : 10,
				}
			};
		},
		
		onLoad(options) {
			// console.log(options);
			this.queryObj.query = options.query || "",
			this.queryObj.cid = options.cid || ""
			
			
			this.getGoodsList()
		},
		
		methods: {
			async getGoodsList(cb){
				//开启节流阀
				this.isloading = true
				
				const {data : res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				//关闭节流阀
				this.isloading = false
				cb && cb()
				
				if(res.meta.status !== 200) return uni.$showMsg()
				
				this.goodList = [...this.goodList, ...res.message.goods]
				this.total = res.message.total
			},
			
			gotoDetail(goods){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id=" + goods.goods_id
				})
			}
		},
		// 监听上拉触底刷新
		onReachBottom() {
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg("数据请求完毕")
			
			if(this.isloading) return
			// 让页码值自增加一
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		
		//下拉刷新
		onPullDownRefresh() {
			// 重置数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goodList = []
			// 重新发起请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">

</style>
